<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          type="primary"
          @click="handleSearchList('searchForm')"
          size="small"
        >
          查询搜索
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          :rules="queryRules"
          ref="searchForm"
          size="small"
          label-width="140px"
        >
          <el-form-item label="输入搜索：">
            <el-input
              v-model="listQuery.title"
              placeholder="资源标题"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="资源ID：" prop="urid">
            <el-input
              v-model="listQuery.urid"
              placeholder="资源ID"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="资源类型：" prop="resourceType">
            <el-select
              v-model="listQuery.resourceType"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="item in resourceTypeOptions"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="发布者ID：" prop="uuid">
            <el-input
              v-model="listQuery.uuid"
              placeholder="发布者ID"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="发布者昵称：">
            <el-input
              v-model="listQuery.userNickname"
              placeholder="发布者昵称"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="资源地址：">
            <el-input
              v-model="listQuery.location"
              placeholder="发布者昵称"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="最低价格：" prop="start">
            <el-input
              v-model="listQuery.start"
              placeholder="最低价格"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="最高价格" prop="end">
            <el-input
              v-model="listQuery.end"
              placeholder="最高价格"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div class="table-container">
      <el-tabs v-model="statusTab" type="card" @tab-click="handleClickTabs">
        <el-tab-pane
          :label="'全部(' + countMap.allCount + ')'"
          name="-1"
        ></el-tab-pane>
        <el-tab-pane
          :label="'已下架(' + countMap.offTheShelfCount + ')'"
          name="40"
        ></el-tab-pane>
        <el-tab-pane
          :label="'审核通过(' + countMap.approvedCount + ')'"
          name="30"
        ></el-tab-pane>
        <el-tab-pane
          :label="'待审核(先审后发)(' + countMap.reviewIssueCount + ')'"
          name="20"
        ></el-tab-pane>
        <el-tab-pane
          :label="'待审核(先发后审)(' + countMap.issueReviewCount + ')'"
          name="10"
        ></el-tab-pane>
        <el-tab-pane
          :label="'审核未通过(' + countMap.notPassedCount + ')'"
          name="0"
        ></el-tab-pane>
      </el-tabs>
      <el-table
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column
          type="selection"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column label="唯一标识" width="180" align="center">
          <template slot-scope="scope">{{ scope.row.urid }}</template>
        </el-table-column>
        <el-table-column label="图片" width="120" align="center">
          <template slot-scope="scope"
            ><el-image
              style="height: 80px"
              fit="contain"
              :src="scope.row.cover"
              :preview-src-list="[scope.row.cover]"
          /></template>
        </el-table-column>
        <el-table-column label="资源标题" align="center">
          <template slot-scope="scope"
            ><b>【{{ scope.row.resourceTypeString }}】{{
              scope.row.title
            }}</b></template
          >
        </el-table-column>
        <el-table-column label="资源地址" width="120" align="center">
          <template slot-scope="scope">{{ scope.row.location }}</template>
        </el-table-column>
        <el-table-column label="资源价格" width="100" align="center">
          <template slot-scope="scope"
            ><div style="color: red">￥{{ scope.row.price }}</div></template
          >
        </el-table-column>
        <el-table-column label="发布者" width="100" align="center">
          <template slot-scope="scope"> {{ scope.row.userNickname }}</template>
        </el-table-column>
        <el-table-column label="发布时间" width="160" align="center">
          <template slot-scope="scope">
            {{ scope.row.ctime }}
          </template>
        </el-table-column>
        <el-table-column label="修改时间" width="160" align="center">
          <template slot-scope="scope">
            {{ scope.row.utime }}
          </template>
        </el-table-column>
        <el-table-column label="审核状态" width="110" align="center">
          <template slot-scope="scope">
            <p
              :class="
                scope.row.status === 30 ? 'success-status' : 'error-status'
              "
            >
              {{ scope.row.status | statusFilter }}
            </p>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleVerify(scope.$index, scope.row)"
              >审核
            </el-button>
            <el-button
              size="mini"
              type="primary"
              @click="handleViewDetail(row.urid)"
              >查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-operate-container">
      <el-select size="small" v-model="operateType" placeholder="批量操作">
        <el-option
          v-for="item in operates"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-button
        style="margin-left: 20px"
        class="search-button"
        @click="handleBatchOperate()"
        type="primary"
        size="small"
      >
        确定
      </el-button>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 20, 50]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog title="审核" :visible.sync="verifyDialogVisible" width="30%">
      <el-form :model="verifyForm" label-width="80px">
        <el-form-item label="审核结果">
          <el-radio-group v-model="verifyForm.verifyStatus" size="medium">
            <el-radio :label="40">下架</el-radio>
            <el-radio :label="30">通过</el-radio>
            <el-radio :label="0">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            placeholder="请输入内容"
            v-model="verifyForm.detail"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleVerifySubmit" size="small"
          >确 定</el-button
        >
        <el-button @click="verifyDialogVisible = false" size="small"
          >取 消</el-button
        >
      </div>
    </el-dialog>
    <el-dialog
      title="资源详情"
      :visible.sync="detailDialogVisible"
      center
      width="70%"
    >
      <div class="item-body">
        <el-image
          fit="scale-down"
          style="margin-right: 10px;"
          :src="agriResourceDetail.cover"
          :preview-src-list="[agriResourceDetail.cover]"
        >
          <div slot="error" class="image-slot">
            <i class="el-icon-picture-outline"></i>
          </div>
        </el-image>
        <el-descriptions title="资源基本信息" :column="2" border>
          <el-descriptions-item
            label="资源名称"
            span="3"
            :labelStyle="{ 'font-size': '18px' }"
            :contentStyle="{ 'font-size': '18px' }"
          >
            【{{ agriResourceDetail.resourceTypeString }}】
            {{ agriResourceDetail.title }}
          </el-descriptions-item>
          <el-descriptions-item label="价格" :contentStyle="{ color: 'red' }">
            ￥ {{ agriResourceDetail.price }}
          </el-descriptions-item>
          <el-descriptions-item label="链接">
            <el-tooltip content="可能链接至淘宝、京东等平台" placement="bottom" effect="light">
            <a
              :href="agriResourceDetail.url"
              target="_blank"
              style="color: blue"
              >外部链接</a
            >
            </el-tooltip>
          </el-descriptions-item>
          <el-descriptions-item label="联系方式">
            {{ agriResourceDetail.contact }}
          </el-descriptions-item>
          <el-descriptions-item label="上传人">
            <a @click="redirectToAccount(agriResourceDetail.uuid)">{{
              agriResourceDetail.userNickname
            }}</a>
          </el-descriptions-item>
          <el-descriptions-item label="地址">
            {{ agriResourceDetail.location }}
          </el-descriptions-item>
          <el-descriptions-item label="发布时间">
            {{ agriResourceDetail.ctime }}
          </el-descriptions-item>
          <el-descriptions-item label="标签" span="2">
            <el-tag
              size="small"
              style="margin-right: 10px"
              v-for="(tag, index) in agriResourceDetail.tags"
              :key="index"
            >
              {{ tag }}
            </el-tag>
          </el-descriptions-item>

          <el-descriptions-item label="简介" span="2">
            {{ agriResourceDetail.remark }}
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { fetchList, updatVerifyStatus, getCounts, getAgriResource } from "@/api/agriResource";
import { getResourceTypes } from "@/api/enum.js";
import { validateNumber } from "@/utils/validate.js";
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  urid: null,
  uuid: null,
  userNickname: null,
  status: null,
  title: null,
  resourceType: null,
  end: null,
  start: null,
  location: null,
};
const defaultAgriResourceDetail = {
  urid: null,
  uuid: null,
  resourceTypeString: null,
  cover: null,
  price: null,
  url: null,
  contact: null,
  userAvatar: null,
  userNickname: null,
  location: null,
  title: null,
  clickCount: null,
  ctime: null,
  remark: null,
  tags: [],
};
export default {
  data() {
    var checkNumber = (rule, value, callback) => {
      if(!value) {
        callback();
      }
      if (!validateNumber(value)) {
        callback(new Error("请输入数字值"));
      } else {
        callback();
      }
    };
    return {
      listQuery: Object.assign({}, defaultListQuery),
      queryRules: {
        urid: [{ validator: checkNumber }],
        resourceType: [{ validator: checkNumber }],
        uuid: [{ validator: checkNumber}],
        start: [{ validator: checkNumber}],
        end: [{ validator: checkNumber}],
      },
      list: null,
      total: null,
      listLoading: false,
      multipleSelection: [],
      operates: [
        {
          label: "批量审核",
          value: "verify",
        },
      ],
      operateType: "verify",
      resourceTypeOptions: null,
      statusTab: "-1",
      countMap: {
        allCount: 0,
        offTheShelfCount: 0,
        approvedCount: 0,
        reviewIssueCount: 0,
        issueReviewCount: 0,
        notPassedCount: 0,
      },
      verifyForm: {
        urids: [],
        verifyStatus: 30,
        detail: "",
      },
      verifyDialogVisible: false,
      detailDialogVisible: false,
      agriResourceDetail: Object.assign({}, defaultAgriResourceDetail)
    };
  },
  created() {
    this.getList();
    this.initResourceTypeOptions();
    this.getCountMap();
  },
  filters: {
    statusFilter(value) {
      if (value === 40) {
        return "已下架";
      } else if (value === 30) {
        return "审核通过";
      } else if (value === 20) {
        return "待审核";
      } else if (value === 10) {
        return "待审核";
      } else {
        return "审核未通过";
      }
    },
  },
  methods: {
    initResourceTypeOptions() {
      getResourceTypes().then((res) => {
        this.resourceTypeOptions = res.data;
      });
    },
    handleClickTabs(tab, event) {
      var status = Number(this.statusTab);
      if (status !== -1) {
        this.listQuery.status = status;
      } else {
        this.listQuery.status = null;
      }
      this.getList();
      this.getCountMap();
    },
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
      this.statusTab = "-1";
    },
    handleSearchList(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.listQuery.pageNum = 1;
          this.statusTab = "-1";
          this.getList();
          this.getCountMap();
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleBatchOperate() {
      if (this.operateType == null) {
        this.$message({
          message: "请选择操作类型",
          type: "warning",
          duration: 1000,
        });
        return;
      }
      if (this.multipleSelection < 1) {
        this.$message({
          message: "请选择一条记录",
          type: "warning",
          duration: 1000,
        });
        return;
      }
      let urids = [];
      for (let i = 0; i < this.multipleSelection.length; i++) {
        urids.push(this.multipleSelection[i].urid);
      }
      if (this.operateType === "verify") {
        this.verifyDialogVisible = true;
        this.verifyForm.urids = urids;
      }
    },
    handleVerify(index, row) {
      this.verifyDialogVisible = true;
      this.verifyForm.urids = [row.urid];
    },
    handleVerifySubmit() {
      let params = new URLSearchParams();
      params.append("urids", this.verifyForm.urids);
      params.append("verifyStatus", this.verifyForm.verifyStatus);
      params.append("detail", this.verifyForm.detail);
      updatVerifyStatus(params).then((res) => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000,
        });
        this.verifyDialogVisible = false;
        this.getList();
        this.getCountMap();
      });
    },
    redirectToAccount(uuid) {
      let url = this.$router.resolve({
        name: "account",
        params: { uuid: uuid },
      });
      window.open(url.href, "_blank");
    },
    handleViewDetail(urid) {
      // 查看详情
      getAgriResource(urid).then((res) => {
        this.agriResourceDetail = res.data;
        this.detailDialogVisible = true;
      });
    },
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then((response) => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
    getCountMap() {
      getCounts(this.listQuery).then((res) => {
        this.countMap = res.data;
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.input-width {
  width: 203px;
}
.success-status {
  background-color: #67c23a;
  color: white;
}
.error-status {
  background-color: #f56c6c;
  color: white;
}
.item-body {
  display: flex;
  justify-content: space-between;
  > * {
    flex: 1;
  }
}
</style>


